import { Base } from './base';

export type UserOrdersQueryBuilder = {
  [key: string]: any;
  offset?: number, limit?: number
}

export class Order extends Base {
  private readonly urls: Record<string, string>;

  constructor(baseUrl: string, {
    headers,
    urls,
  }: { headers?: Record<string, string>, urls?: Record<string, string> }) {
    super(baseUrl);
    if (headers)
      this.setDefaultHeaders(headers);
    this.urls = urls || {
      createUserOrder: '/orders/user',
      findUserOrders: '/orders/user',
      getUserOrderById: '/orders/user',
    };
  }

  getUrls() {
    return this.urls;
  }

  async createUserOrder(terminalOrderId: string, orderData: Record<string, string>) {
    return await super.post(this.urls.createUserOrder, { terminalOrderId, ...orderData });
  }

  async findUserOrders({ offset = 0, limit = 10, ...query }: UserOrdersQueryBuilder) {
    return await super.get(this.urls.findUserOrders, {
      offset,
      limit, ...query,
    });
  }

  async getSelfOrderById(orderId: string, params?: { terminalOrderId?: string }) {
    return await super.get(`${this.urls.getUserOrderById}/${orderId}/self`, params);
  }

  async getUserOrderById(orderId: string, params?: { terminalOrderId?: string }) {
    return await super.get(`${this.urls.getUserOrderById}/${orderId}`, params);
  }

}